package com.example.myapplication.activity;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.example.myapplication.R;
import com.example.myapplication.bean.His;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import android.widget.TextView;
import android.widget.Toast;
import android.content.SharedPreferences;
import okhttp3.Call;

import android.widget.ArrayAdapter;


public class activity_history extends AppCompatActivity {

    private TextView tv_mian_bar;
    private Button btn;
    private Button tv_back;
    private String username;
    private ListView lis;
    private His[] his;
    //private ArrayAdapter<His> sa;
    //    定义一个日志信息过滤标签，测试用
    private static final String  TAG="DBUtils";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);

        //去除默认标题栏
        ActionBar actionBar=getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }

        // DBUtiles db=new DBUtiles();
        init();
        empty();
    }

    private void init(){
        tv_mian_bar = (TextView) findViewById(R.id.tv_main_title);
        tv_mian_bar.setText("History");

        tv_back = (Button) findViewById(R.id.tv_back);
        btn=(Button)findViewById(R.id.clear_his);
        //返回按钮监听
        tv_back.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                activity_history.this.finish();
            }
        });
//展示历史记录
        lis=(ListView)findViewById(R.id.listView_his) ;

        //获取userid
        SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        //SharedPreferences.Editor editor=sp.edit();//获取编辑器
        username=sp.getString("loginUserName","default");

        OkHttpUtils
                .get()
                .url("http://121.36.92.128:8086/history")
                .addParams("userid", username)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        System.out.println(e.toString());
                    }

                    @Override
                    public void onResponse(String response, int id) {

                        if (!response.equals("[]")) {
                            Gson gson = new Gson();
                            his = gson.fromJson(response, His[].class);

                            ArrayAdapter<His> adapter = new ArrayAdapter<His>(activity_history.this, android.R.layout.simple_list_item_1, his);
                            //sa=adapter;
                            lis.setAdapter(adapter);

                        }  else {
                            Toast.makeText(activity_history.this, "你还没有历史记录，快开始搜索吧！", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
    private void empty(){
//清空
       //btn=(Button) findViewById(R.id.clear_his);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //调后端函数
                //获取userid
                SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
                //SharedPreferences.Editor editor=sp.edit();//获取编辑器
                username=sp.getString("loginUserName","default");

                OkHttpUtils
                        .post()
                        .url("http://121.36.92.128:8086/history/delete")
                        .addParams("userid", username)
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                System.out.println(e.toString());
                            }

                            @Override
                            public void onResponse(String response, int id) {

                                if (response.equals("true")) {

                                    /*sa.clear();
                                    sa.notifyDataSetChanged();
                                    lis.setAdapter(sa);
                                    Toast.makeText(activity_history.this, "清空成功", Toast.LENGTH_SHORT).show();*/
                                    his = new His[0]; // 创建一个空的 His 数组来清空历史记录
                                    ArrayAdapter<His> adapter = new ArrayAdapter<His>(activity_history.this, android.R.layout.simple_list_item_1, his);
                                    //sa=adapter;
                                    lis.setAdapter(adapter);
                                    Toast.makeText(activity_history.this, "历史记录已清空！", Toast.LENGTH_SHORT).show();
                                }  else {
                                    Toast.makeText(activity_history.this, "清空失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
            }
        });
    }


}